package ua.kiev.tosik.price.conf;

import org.apache.log4j.Logger;

import javax.swing.*;
import java.util.Locale;
import java.util.ResourceBundle;

public class Language {

	private static Language ourInstance;
	// private static File file = new File("conf/keywords_ru.properties");
	private static final String KEYWORDS_FILE = "ua.kiev.tosik.price.conf.keywords";
	private static Logger logger = Logger.getLogger("ua.kiev.tosik.price.conf");
	private ResourceBundle resourceBundle;
	private Locale locale;
	// private Vector<Localizable> localizables = new Vector<Localizable>();
	private boolean status = false;
	private String message;

	public static Language getInstance() {
		if ( ourInstance == null ) {
			ourInstance = new Language();
		}
		return ourInstance;
	}

	private Language() {
		logger.debug("Init translations");
		locale = new Locale(AppConfig.getInstance().get("app.language"));
		try {
			resourceBundle = ResourceBundle.getBundle(KEYWORDS_FILE, locale);
			status = true;
		} catch (Exception e) {
			status = false;
			message = e.getLocalizedMessage();
			logger.fatal(e.getLocalizedMessage());
		}

		UIManager.put("FileChooser.openButtonText", get("FileChooser.openButtonText"));
		UIManager.put("FileChooser.saveButtonText", get("FileChooser.saveButtonText"));
		UIManager.put("FileChooser.cancelButtonText", get("FileChooser.cancelButtonText"));
		UIManager.put("FileChooser.helpButtonText", get("FileChooser.helpButtonText"));
		UIManager.put("FileChooser.updateButtonText", get("FileChooser.updateButtonText"));
		UIManager.put("FileChooser.fileNameLabelText", get("FileChooser.fileNameLabelText"));
		UIManager.put("FileChooser.homeFolderToolTipText", get("FileChooser.homeFolderToolTipText"));
		UIManager.put("FileChooser.lookInLabelText", get("FileChooser.lookInLabelText"));
		UIManager.put("FileChooser.filesOfTypeLabelText", get("FileChooser.filesOfTypeLabelText"));

		UIManager.put("OptionPane.yesButtonText", get("OptionPane.yesButtonText"));
		UIManager.put("OptionPane.noButtonText", get("OptionPane.noButtonText"));
		UIManager.put("OptionPane.cancelButtonText", get("OptionPane.cancelButtonText"));

		UIManager.put("JXTable.column.horizontalScroll", get("JXTable.column.horizontalScroll"));
		
		UIManager.put("Search.notFound", get("Search.notFound"));
		UIManager.put("Search.searchFieldLabel", get("Search.searchFieldLabel"));
		UIManager.put("Search.searchTitle", get("Search.searchTitle"));
		UIManager.put("Search.backwardsSearch", get("Search.backwardsSearch"));
		UIManager.put("Search.findNext", get("Search.findNext"));
		UIManager.put("Search.matchCase", get("Search.matchCase"));
		UIManager.put("Search.close", get("Search.close"));
		UIManager.put("Search.match", get("Search.match"));
		UIManager.put("XDialog.close", get("XDialog.close"));
		UIManager.put("XDialog.cancel", get("XDialog.cancel"));
		UIManager.put("XDialog.execute", get("XDialog.execute"));

		UIManager.put("JXDatePicker.linkFormat", get("JXDatePicker.linkFormat"));

		UIManager.put("JXErrorPane.ok_button_text", get("JXErrorPane.ok_button_text"));
		UIManager.put("JXErrorPane.details_expand_text", get("JXErrorPane.details_expand_text"));
		UIManager.put("JXErrorPane.details_contract_text", get("JXErrorPane.details_contract_text"));
		UIManager.put("JXErrorPane.fatal_button_text", get("JXErrorPane.fatal_button_text"));
		UIManager.put("JXErrorPane.report_button_text", get("JXErrorPane.report_button_text"));
		UIManager.put("JXErrorPane.copy_to_clipboard_button_text", get("JXErrorPane.copy_to_clipboard_button_text"));

	}

	/*
	public void reload() {
		status = false;
		logger.debug("Reloading translations");
		try {
			resourceBundle = ResourceBundle.getBundle(KEYWORDS_FILE, locale);
			for ( Localizable l: localizables ) {
				logger.debug("Reloading translations for " + l.toString());
				try {
					l.updateLocale();
				} catch (Exception e) {
					message = e.getLocalizedMessage();
					logger.error(e.getLocalizedMessage());
				}
			}
			status = true;
		} catch (Exception e) {
			message = e.getLocalizedMessage();
			logger.fatal(e.getLocalizedMessage());
		}
	} */

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	/*
	public Vector<Localizable> getLocalizables() {
		return localizables;
	} */

	public String get(String key) {
		return resourceBundle.getString(key);
	}

	public boolean isStatus() {
		return status;
	}

	public String getMessage() {
		return message;
	}
}
